package com.zhangwei.expands.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zhangwei.expands.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @version V1.0
 * @description:
 * @title: Expands
 * @package com.zhangwei.expands.adapter
 * @author: zhangwei
 * @date: 2015-12-16 上午9:22
 */
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
    private List<String> list;


    private OnRecyclerItemClick itemClick;


    public MyRecyclerAdapter(List<String> list) {
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_layout, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClick != null) {
                    itemClick.onRecyclerItemClick(holder, list.get(position), position);
                }
            }
        });
        holder.nameTxt.setText(list.get(position));


    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item_list_txt)
        TextView nameTxt;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnRecyclerItemClick {
        void onRecyclerItemClick(MyViewHolder holder, String str, int position);
    }

    public OnRecyclerItemClick getItemClick() {
        return itemClick;
    }

    public void setItemClick(OnRecyclerItemClick itemClick) {
        this.itemClick = itemClick;
    }


    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }
}
